home *** CD-ROM | disk | FTP | other *** search
- /* vfscanf.c --- p 476 */
- #include <stdio.h>
- #include <stdarg.h>
- void getfromfile(FILE *, char *,...);
- main()
- {
- char filename[80], dbname[80], pname[80],
- mname[80], partno[40];
- FILE *datafile;
- double uprice;
- /* Get file name and open it for reading */
- printf("Enter name of data file:");
- gets(filename);
- if((datafile = fopen(filename, "r")) == NULL)
- {
- printf("Error opening file: %s\n", filename);
- exit(1);
- }
- /* Call the input routine to read values of variables.
- * First just a single number. Then more than one value. */
- getfromfile(datafile, "DATABASE: %[^\n] ", &dbname);
- getfromfile(datafile, "Product Name: %[^\n] Manufacturer: %[^\n] "
- "Part #: %s Unit Price: $%lf",
- &pname, &mname, &partno, &uprice);
- printf("The first record in %s contain:\n Product Name: %s\n"
- "Manufacturer: %s\n"
- "Part #: %s\n"
- "Unit Price: $%.2lf\n", dbname, pname,
- mname, partno, uprice);
- }
- /*-------------------------*/
- /* getfromfile: accepts variable number of arguments
- * and reads formatted values from a file */
- void getfromfilef(FILE *stream, char *my_format,...)
- {
- va_list arg_pointer;
- /* Use va_start macro to get to the start of the
- * variable number of arguments. This will alter the
- * pointer arg_pointer to point to the list of
- * variables to be read. */
- va_startf(arg_pointer, my_format);
- vfscanf(stream, my_format, arg_pointer);
- /* Use the va_end macro to reset the arg_pointer */
- va_end(arg_pointer);
- }